package com.whw.fy.service.impl;

import com.whw.fy.config.GlobalConfig;
import com.whw.fy.entity.*;
import com.whw.fy.mapper.*;
import com.whw.fy.utils.ExcelReadUtil;
import com.whw.fy.utils.WordUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;

import com.whw.fy.service.RecordService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class RecordServiceImpl implements RecordService {

    @Resource
    private RecordMapper recordMapper;
    @Resource
    private StaffMapper staffMapper;
    @Resource
    private LessonMapper lessonMapper;
    @Resource
    private RoomMapper roomMapper;
    @Resource
    private StudentMapper studentMapper;

    @Resource
    private GlobalConfig globalConfig;



    @Override
    public Message deleteByPrimaryKey(Integer recordId) {
        return recordMapper.deleteByPrimaryKey(recordId) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insert(Record record) {
        return recordMapper.insert(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message insertSelective(Record record) {
        return recordMapper.insertSelective(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectByPrimaryKey(Integer recordId) {
        Record record = recordMapper.selectByPrimaryKey(recordId);
        return Message.success(null).add(recordProd(record));
    }

    @Override
    public Message updateByPrimaryKeySelective(Record record) throws ParseException {
        if (record.getRecordListenTimeToString() != null) {
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            record.setRecordListentime(simpleDateFormat.parse(record.getRecordListenTimeToString()));
        }
        return recordMapper.updateByPrimaryKeySelective(record) > 0 ? Message.success(null) : Message.fail(null);

    }

    @Override
    public Message updateByPrimaryKey(Record record) {
        return recordMapper.updateByPrimaryKey(record) > 0 ? Message.success(null) : Message.fail(null);
    }

    @Override
    public Message selectAll() {
        List<Record> allList = recordMapper.selectAll();
        for(Record record : allList){
            recordProd(record);
        }
        return Message.success(null).add(allList);
    }

    @Override
    public Message selectByRecordStudentId(int recordStudentId) {
        List<Record> RecordList=recordMapper.selectByRecordStudentId(recordStudentId);
        for(Record record : RecordList){
            recordProd(record);
        }
        return Message.success(null).add(RecordList);
    }

    @Override
    public Message downloadByPrimaryKey(String StudentId) throws Exception {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat simpleDateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:dd:SS");
        Record record = recordMapper.selectByPrimaryKey(Integer.parseInt(StudentId));
        recordProd(record);
        HashMap<String, Object> Recordmap = new HashMap();
        Recordmap.put("stuName",record.getStudentName());
        Recordmap.put("classType",record.getRecordLessonName());
        Recordmap.put("classTeach",record.getRecordMainteacherName());
        Recordmap.put("staffName",record.getRecordSuperviseteacherName());
        Recordmap.put("className",record.getRecordLessonName());
        Recordmap.put("date",simpleDateFormat2.format(record.getRecordListentime()));
        Recordmap.put("teachInfo",record.getRecordTeachcontent());
        Recordmap.put("teachAvg",record.getRecordTeachadvantage());
        Recordmap.put("teachProb",record.getRecordTeachquestion());
        Recordmap.put("teachAdvice",record.getRecordGuildsuggestion());

        String url = WordUtil.getWordUrl(Recordmap,simpleDateFormat.format(record.getRecordListentime())+record.getStudentName()+"听课记录.doc","模板10.xml",globalConfig.hostUrl,globalConfig.globalFilePath);
        return Message.success(null).add(url);
    }



    @Override
    public Message insertByReadExcel(String fileUrl){
//        List<List<Object>> excelData = ExcelReadUtil.getExcelData(fileUrl);
//        excelData = excelDataDuplicate(excelData);
//        Record record = new Record();
//
//        Student student=studentMapper.selectByStudentName(excelData.get(1).get(2).toString().toString());
//        record.setRecordStudentId(student.getStudentId());
        return Message.success(null).add("待定");
    }




    /**
     * 对Record对象进行加工主体
     * @param record
     * @return
     */
    private Record recordProd(Record record){
        try{
            Staff staff1 =staffMapper.selectByPrimaryKey(record.getRecordMainteacherId());
            record.setRecordMainteacherName(staff1.getStaffName());
            Staff staff2 =staffMapper.selectByPrimaryKey(record.getRecordSuperviseteacherId());
            record.setRecordSuperviseteacherName(staff2.getStaffName());
            Lesson lesson =lessonMapper.selectByPrimaryKey(record.getRecordLessonId());
            record.setRecordLessonName(lesson.getLessonName());
            Room room=roomMapper.selectByPrimaryKey(record.getRecordClassroomId());
            record.setRecordClassroomName(room.getRoomLoc());
            Student student = studentMapper.selectByPrimaryKey(record.getRecordStudentId());
            record.setStudentName(student.getStudentName());
        }catch (Exception e){
            return record;
        }
        return record;
    }




    /**
     * 对Excel的数据的list的obj 进行去重
     * @param list
     * @return
     */
    private List excelDataDuplicate(List list){
        List listTemp  = new ArrayList();
        for(int i=0;i<list.size();i++){
            if(!listTemp.contains(list.get(i))){
                listTemp.add(list.get(i));
            }
        }
        return listTemp;
    }

}



